//
//  FEViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "FEViewController.h"
#import "UrlRequestManager.h"
#import "HeaderModel.h"
#import "HeaderNewsTableViewCell.h"
#import "WebViewController.h"
#import "WheelImageTool.h"
#import "LORefresh.h"
#import "SecondHeaderNewsTableViewCell.h"

@interface FEViewController ()<UITableViewDataSource,UITableViewDelegate>
//存放轮播图片URL的数组
@property(nonatomic,strong)NSMutableArray *arrPlayerModel;
//存放所有新闻的数组
@property(nonatomic,strong)NSMutableArray *arrAllData;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation FEViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self requestData];
    [self refreshAndLoad];
}
//上拉加载，下拉刷新
- (void)refreshAndLoad{
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.defaultFooter endRefreshing];
    }];
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [weakSelf.arrAllData removeAllObjects];
        [weakSelf requestData];
        
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.gifHeader endRefreshing];
    }];
    [self.tableView.gifHeader setGifName:@"demo.gif"];
    self.tableView.gifHeader.refreshLayoutType = LORefreshLayoutTypeRightIndicator;
    self.tableView.defaultFooter.refreshLayoutType = LORefreshLayoutTypeRightIndicator;
}
- (void)initView{
    self.title = @"财经新闻";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.navigationController.navigationBar.translucent = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"HeaderNewsTableViewCell"bundle:nil] forCellReuseIdentifier:@"HeaderNewsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondHeaderNewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"SecondHeaderNewsTableViewCell"];
}
- (void)requestData{
    __weak typeof(self) weakSelf = self;
    [UrlRequestManager requestWith:GET URL:kFEUrl condition:nil SuccessBlock:^(id item)
     {
         for (NSDictionary *dicTemp in item[@"T1348648756099"])
         {
             NSArray *arr = dicTemp[@"ads"];
             if (arr)
             {//数据中有ads对应的内容才走这个方法
                 for (NSDictionary *dic in arr)
                 {
                     HeaderModel *model = [[HeaderModel alloc]init];
                     [model setValuesForKeysWithDictionary:dic];
                     [weakSelf.arrPlayerModel addObject:model];
                 }
             }
             HeaderModel *mode = [[HeaderModel alloc]init];
             [mode setValuesForKeysWithDictionary:dicTemp];
             [weakSelf.arrAllData addObject:mode];
         }
         dispatch_async(dispatch_get_main_queue(), ^{
             //主线程称中刷新界面
             [self.tableView reloadData];
         });
     } failBlock:^(NSError *error)
     {
         NSLog(@"%@",error);
     }];
}
//tableViewDelegate/tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrAllData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HeaderModel *model = self.arrAllData[indexPath.row];
    if (!model.imgextra) {
        HeaderNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeaderNewsTableViewCell" forIndexPath:indexPath];
        cell.title.text = model.title;
        cell.digest.text = model.digest;
        cell.replyCount.text = [NSString stringWithFormat:@"%ld",model.replyCount];
        cell.imV.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgsrc]]];
        return cell;
    }else{
        SecondHeaderNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondHeaderNewsTableViewCell" forIndexPath:indexPath];
        cell.title.text = model.title;
        cell.replyCount.text = [NSString stringWithFormat:@"%ld",model.replyCount];
        cell.imVLeft.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgsrc]]];
        cell.imVMid.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgextra[0][@"imgsrc"]]]];
        cell.imVRight.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.imgextra[1][@"imgsrc"]]]];
        return cell;
    }
}
//头视图返回轮播图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    WheelImageTool *tool = [[WheelImageTool alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 150) imageUrlArr:self.arrPlayerModel timeInterval:2];
    return tool;
}
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 150;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
//选中cell时的跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HeaderModel *model = self.arrAllData[indexPath.row];
    WebViewController *webV = [[WebViewController alloc]init];
    webV.url_3w = model.url_3w;
    webV.header = model.title;
    [self.navigationController pushViewController:webV animated:YES];
}
#pragma mark - 懒加载
- (NSMutableArray *)arrAllData{
    if (!_arrAllData) {
        _arrAllData = [NSMutableArray array];
    }
    return _arrAllData;
}
- (NSMutableArray *)arrPlayerModel{
    if (!_arrPlayerModel) {
        _arrPlayerModel = [NSMutableArray array];
    }
    return _arrPlayerModel;
}

@end
